﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RioStudio.Website.Model;

namespace RioStudio.Website.View.Admin.Gallery
{
    public partial class Detail : System.Web.UI.Page
    {
        private Guid GalleryID { get; set; }

        public bool IsEdit { get; set; }

        private void GetData(Guid id)
        {
            var model = tData.Instance.ExecuteReader<RioStudio.Website.Model.Gallery>("Proc_GetGallery", id).FirstOrDefault();

            if (model != null)
            {
                //txtServiceName.Text = model.ServiceName;
                txtGalleryName.Text = model.GalleryName;
                txtAvatar.Text = model.GalleryAvatar;
                txtGalleryPlace.Text = model.GalleryPlace;
                txtSortOrder.Value = model.SortOrder;

                chkInactive.Checked = model.Inactive;
                chkIsFeatured.Checked = model.IsFeatured;

                rdpGalleryDate.SelectedDate = model.GalleryDate;
                //editorDetail.Content = model.Detail;
            }
        }

        private void BindingImage(Guid id)
        {
            dataTest.DataSource = tData.Instance.ExecuteReader<RioStudio.Website.Model.GalleryImage>("Proc_GetGalleryImageByAlbumID", id);
            dataTest.DataBind();
        }

        private void GetImage(Guid id)
        {
            var model = tData.Instance.ExecuteReader<RioStudio.Website.Model.GalleryImage>("Proc_GetGalleryImage", id).FirstOrDefault();

            if (model != null)
            {
                txtImageName.Text = model.ImageName;
                txtImageLink.Text = model.ImageLink;
                txtImageSortOrder.Value = model.SortOrder;
                chkImageInactive.Checked = model.Inactive;
                hiddenImageID.Value = model.ImageID.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.IsEdit = false;
            rdpGalleryDate.MinDate = new DateTime();
            if (Request.Params["key"] != null)
            {
                this.IsEdit = true;
                this.GalleryID = new Guid(Request.Params["key"]);

            }
            if (!IsPostBack)
            {
                if (IsEdit)
                {
                    GetData(this.GalleryID);
                    //btnAddImage.Visible = true;
                    panelImage.Visible = true;
                    btnAddImage.Visible = true;
                    BindingImage(this.GalleryID);
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var model = new RioStudio.Website.Model.Gallery();

            model.GalleryName = txtGalleryName.Text;
            model.GalleryAvatar = txtAvatar.Text;
            model.GalleryPlace = txtGalleryPlace.Text;
            model.SortOrder = (int)txtSortOrder.Value;
            model.GalleryID = Guid.NewGuid();
            model.Inactive = chkInactive.Checked;
            model.IsFeatured = chkIsFeatured.Checked;
            model.GalleryDate = rdpGalleryDate.SelectedDate.Value;
            model.Slug = txtGalleryName.Text.RemoveSign4VietnameseString().Replace(" ", "-");
            model.Description = txtDescription.Text;

            try
            {
                if (IsEdit)
                {
                    model.ModifiedBy = User.Identity.Name;
                    model.ModifiedDate = DateTime.Now;
                    model.GalleryID = GalleryID;
                    
                    tData.Instance.ExecuteNonQuery<RioStudio.Website.Model.Gallery>("Proc_UpdateGallery", model);
                    BindingImage(this.GalleryID);
                }
                else
                {
                    model.CreatedBy = User.Identity.Name;
                    model.CreatedDate = DateTime.Now;

                    tData.Instance.ExecuteNonQuery<RioStudio.Website.Model.Gallery>("Proc_InsertGallery", model);

                    Response.Redirect("/Admin/Gallery/Edit/" + model.GalleryID.ToString());
                }


            }
            catch (Exception)
            {

                throw;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                var model = new RioStudio.Website.Model.GalleryImage();
                model.ImageName = txtImageName.Text;
                model.Slug = txtImageName.Text.RemoveSign4VietnameseString().Replace(" ", "-");
                model.ImageLink = txtImageLink.Text;
                model.Inactive = chkImageInactive.Checked;
                model.SortOrder = (int)txtImageSortOrder.Value;

                if (!string.IsNullOrEmpty(hiddenImageID.Value))
                {
                    model.ModifiedBy = User.Identity.Name;
                    model.ModifiedDate = DateTime.Now;

                    tData.Instance.ExecuteNonQuery<RioStudio.Website.Model.GalleryImage>("Proc_UpdateGalleryImage", model);
                }
                else
                {
                    model.ImageID = Guid.NewGuid();
                    model.GalleryID = GalleryID;
                    model.CreatedBy = User.Identity.Name;
                    model.CreatedDate = DateTime.Now;
                    tData.Instance.ExecuteNonQuery<RioStudio.Website.Model.GalleryImage>("Proc_InsertGalleryImage", model);
                }

                BindingImage(GalleryID);

                //ScriptManager.RegisterStartupScript(this, this.GetType(), "CloseWindow", "$find('" + RadWindow_ContentTemplate.ClientID + "').close();", true);
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void btnDeleteImage_Click(object sender, EventArgs e)
        {
            try
            {
                var id = ((Button)sender).CommandArgument;

                var model = new GalleryImage() { ImageID = new Guid(id) };

                tData.Instance.ExecuteNonQuery<GalleryImage>("Proc_DeleteGalleryImage", model);

                BindingImage(this.GalleryID);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}